package com.ruoyi.mes.utils;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 六轴机械臂 (功能比较局限)
 */
public class SixMechanicalArmUtils {
    public static void SixMechanicalArmGrab(String proceName){
            String url =null;
            // 默认请求数据为空JSON对象
            String requestData = "{}";
            if (proceName.equals("六轴机械臂抓")){
                // API地址
                 url = "http://192.168.1.100:5000/mes/robot-arm/6axis";
                // 请求数据
                requestData = "{\"action\":\"take_from_car\"}";
            } else if (proceName.equals("六轴机械臂放")) {
                // API地址
                 url = "http://192.168.1.100:5000/mes/robot-arm/6axis";
                // 请求数据
                requestData = "{\"action\":\"put_to_car\"}";
            }
            else//如果不是抓和放，直接退出
            {
                return;
            }

//             请求数据
//            String requestData = "{}";

            try {
                // 创建URL对象
                URL apiUrl = new URL(url);
                // 打开URL连接
                HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
                // 设置请求方法为POST
                connection.setRequestMethod("POST");
                // 设置请求头部信息
                connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                // 允许输出流写入数据
                connection.setDoOutput(true);
                // 将请求数据写入输出流
                connection.getOutputStream().write(requestData.getBytes("UTF-8"));
                // 发送HTTP请求
                connection.connect();

                // 读取响应数据
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                // 关闭连接
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}
